package edu.chl.da.ejb.ctrl;

import edu.chl.da.ejb.core.Airport;
import edu.chl.da.ejb.core.Destination;
import edu.chl.da.ejb.persistency.IDestinationFacade;
import edu.chl.da.ejb.persistency.IFlightFacade;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Max
 */
@Stateless
public class ManageDestination implements IManageDestination {

    @EJB
    IDestinationFacade idf;

    @EJB
    IFlightFacade iff;

    @Override
    public Destination createDestination(String name, String image, double geox, double geoy) {
        Destination destination = new Destination();
        destination.setGeox(geox);
        destination.setGeoy(geoy);
        destination.setImage(image);
        destination.setName(name);
        
        List<Airport> airportList = new ArrayList<Airport>();
        destination.setAirportList(airportList);
        
        idf.create(destination);
        return destination;
    }

    @Override
    public Destination editDestination(Destination destination) {
        if(destination == null){
            throw new NullPointerException("Destination is null");
        }
        idf.edit(destination);
        return destination;
    }


    @Override
    public boolean removeDestination(Destination destination) {
        try {
            idf.remove(destination);
        }
        catch(Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<Destination> getAllDestinations() {
        return idf.findAll();
    }

    @Override
    public Destination getDestinationByName(String name) {
        return idf.find(name);
    }

    @Override
    public List<String> getAllDestinationsInString() {
        List<Destination> listDestinations = idf.findAll();
        List<String> temp = new ArrayList<String>();
        for(Destination d : listDestinations){
            temp.add(d.getName());
        }
        return temp;
    }
}
